<?php

class TestAction extends Action{
	
	public function index(){
		$Product = M('Product');
		$productList = $_GET['goods'];
		$querySql = 'SET num = CASE id ';
		$goods = explode(',', $_GET['goods']);
		foreach ($goods as $k=>$v){
			$v = explode('-', $v);
			$productId = $v[0];
			$productNum = $v[1];
			$proArr[$productId] = $productNum;
			$productIds .= $productId .",";
		}
		$productIds = rtrim($productIds,",");
		$where['flag'] = 1;
		$where['id'] = array('in',$productIds);
		$productList = $Product->where($where)->field('id,photo,name,price,is_on,num')->select();
		$querySql = 'SET num = CASE id ';
		foreach ($productList as $pk=>$pv){
			$sellNum = $proArr[$pv['id']];
			//组装扣除抢购数量sql
			$queryIds .= $pv['id'].',';
			$querySql .= ' WHEN '.$pv['id'].' THEN num -'.$sellNum." ";
		}
		$querySql .= ' END ';
		$queryIds = substr($queryIds,0,strlen($queryIds)-1);
		$querySql .= 'where id in ('.$queryIds.')';
		$result = $Product->execute('update __TABLE__ '.$querySql);
		echo $result;
	}
}	